home *** CD-ROM | disk | FTP | other *** search
/ Skunkware 98 / Skunkware 98.iso / src / mail / pine3.96.tar.gz / pine3.96.tar / pine3.96 / imap / ANSI / c-client / news.c < prev    next >
C/C++ Source or Header  |  1996-05-15  |  33KB  |  1,137 lines

  1. /*
  2.  * Program:    Netnews mail routines
  3.  *
  4.  * Author:    Mark Crispin
  5.  *        Networks and Distributed Computing
  6.  *        Computing & Communications
  7.  *        University of Washington
  8.  *        Administration Building, AG-44
  9.  *        Seattle, WA  98195
  10.  *        Internet: MRC@CAC.Washington.EDU
  11.  *
  12.  * Date:    4 September 1991
  13.  * Last Edited:    15 May 1996
  14.  *
  15.  * Copyright 1996 by the University of Washington
  16.  *
  17.  *  Permission to use, copy, modify, and distribute this software and its
  18.  * documentation for any purpose and without fee is hereby granted, provided
  19.  * that the above copyright notice appears in all copies and that both the
  20.  * above copyright notice and this permission notice appear in supporting
  21.  * documentation, and that the name of the University of Washington not be
  22.  * used in advertising or publicity pertaining to distribution of the software
  23.  * without specific, written prior permission.  This software is made
  24.  * available "as is", and
  25.  * THE UNIVERSITY OF WASHINGTON DISCLAIMS ALL WARRANTIES, EXPRESS OR IMPLIED,
  26.  * WITH REGARD TO THIS SOFTWARE, INCLUDING WITHOUT LIMITATION ALL IMPLIED
  27.  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, AND IN
  28.  * NO EVENT SHALL THE UNIVERSITY OF WASHINGTON BE LIABLE FOR ANY SPECIAL,
  29.  * INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
  30.  * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, TORT
  31.  * (INCLUDING NEGLIGENCE) OR STRICT LIABILITY, ARISING OUT OF OR IN CONNECTION
  32.  * WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  33.  *
  34.  */
  35.  
  36. #include <stdio.h>
  37. #include <ctype.h>
  38. #include <errno.h>
  39. extern int errno;        /* just in case */
  40. #include "mail.h"
  41. #include "osdep.h"
  42. #include <sys/stat.h>
  43. #include <sys/time.h>
  44. #include "news.h"
  45. #include "rfc822.h"
  46. #include "misc.h"
  47. #include "newsrc.h"
  48.  
  49. /* Netnews mail routines */
  50.  
  51.  
  52. /* Driver dispatch used by MAIL */
  53.  
  54. DRIVER newsdriver = {
  55.   "news",            /* driver name */
  56.   (DRIVER *) NIL,        /* next driver */
  57.   news_valid,            /* mailbox is valid for us */
  58.   news_parameters,        /* manipulate parameters */
  59.   news_find,            /* find mailboxes */
  60.   news_find_bboards,        /* find bboards */
  61.   news_find_all,        /* find all mailboxes */
  62.   news_find_all_bboards,    /* find all bboards */
  63.   news_subscribe,        /* subscribe to mailbox */
  64.   news_unsubscribe,        /* unsubscribe from mailbox */
  65.   news_subscribe_bboard,    /* subscribe to bboard */
  66.   news_unsubscribe_bboard,    /* unsubscribe from bboard */
  67.   news_create,            /* create mailbox */
  68.   news_delete,            /* delete mailbox */
  69.   news_rename,            /* rename mailbox */
  70.   news_open,            /* open mailbox */
  71.   news_close,            /* close mailbox */
  72.   news_fetchfast,        /* fetch message "fast" attributes */
  73.   news_fetchflags,        /* fetch message flags */
  74.   news_fetchstructure,        /* fetch message envelopes */
  75.   news_fetchheader,        /* fetch message header only */
  76.   news_fetchtext,        /* fetch message body only */
  77.   news_fetchbody,        /* fetch message body section */
  78.   news_setflag,            /* set message flag */
  79.   news_clearflag,        /* clear message flag */
  80.   news_search,            /* search for message based on criteria */
  81.   news_ping,            /* ping mailbox to see if still alive */
  82.   news_check,            /* check for new messages */
  83.   news_expunge,            /* expunge deleted messages */
  84.   news_copy,            /* copy messages to another mailbox */
  85.   news_move,            /* move messages to another mailbox */
  86.   news_append,            /* append string message to mailbox */
  87.   news_gc            /* garbage collect stream */
  88. };
  89.  
  90.                 /* prototype stream */
  91. MAILSTREAM newsproto = {&newsdriver};
  92.  
  93. /* Netnews mail validate mailbox
  94.  * Accepts: mailbox name
  95.  * Returns: our driver if name is valid, NIL otherwise
  96.  */
  97.  
  98. DRIVER *news_valid (char *name)
  99. {
  100.   int fd;
  101.   char *s,*t,*u;
  102.   char tmp[MAILTMPLEN];
  103.   struct stat sbuf;
  104.   DRIVER *ret = NIL;
  105.                 /* looks plausible and news installed? */
  106.   if (name && (*name == '*') && !(strchr (name,'/')) &&
  107.       !stat ((char *) mail_parameters (NIL,GET_NEWSSPOOL,NIL),&sbuf) &&
  108.       ((fd = open ((char *) mail_parameters (NIL,GET_NEWSACTIVE,NIL),O_RDONLY,
  109.            NIL)) >= 0)) {
  110.     fstat (fd,&sbuf);        /* get size of active file */
  111.                 /* slurp in active file */
  112.     read (fd,t = s = (char *) fs_get (sbuf.st_size+1),sbuf.st_size);
  113.     s[sbuf.st_size] = '\0';    /* tie off file */
  114.     close (fd);            /* flush file */
  115.     lcase (strcpy (tmp,name+1));/* make sure compare with lower case */
  116.     while (*t && (u = strchr (t,' '))) {
  117.       *u++ = '\0';        /* tie off at end of name */
  118.       if (!strcmp (tmp,t)) {    /* name matches? */
  119.     ret = &newsdriver;    /* seems to be a valid name */
  120.     break;
  121.       }
  122.       t = 1 + strchr (u,'\n');    /* next line */
  123.     }
  124.     fs_give ((void **) &s);    /* flush data */
  125.  }
  126.   return ret;            /* return status */
  127. }
  128.  
  129.  
  130. /* News manipulate driver parameters
  131.  * Accepts: function code
  132.  *        function-dependent value
  133.  * Returns: function-dependent return value
  134.  */
  135.  
  136. void *news_parameters (long function,void *value)
  137. {
  138.   return NIL;
  139. }
  140.  
  141. /* Netnews mail find list of mailboxes
  142.  * Accepts: mail stream
  143.  *        pattern to search
  144.  */
  145.  
  146. void news_find (MAILSTREAM *stream,char *pat)
  147. {
  148.   /* Always a no-op */
  149. }
  150.  
  151.  
  152. /* Netnews mail find list of bboards
  153.  * Accepts: mail stream
  154.  *        pattern to search
  155.  */
  156.  
  157. void news_find_bboards (MAILSTREAM *stream,char *pat)
  158. {
  159.   char *t;
  160.   void *s = NIL;
  161.   struct stat sbuf;
  162.                 /* read from newsrc if have local news spool */
  163.   if (!(stat ((char *) mail_parameters (NIL,GET_NEWSSPOOL,NIL),&sbuf) ||
  164.     stat ((char *) mail_parameters (NIL,GET_NEWSACTIVE,NIL),&sbuf) ||
  165.     (stream && stream->anonymous))) newsrc_find (pat);
  166.   while (t = sm_read (&s))    /* get data from subscription manager */
  167.     if ((*t == '*') && pmatch (t+1,pat)) mm_bboard (t+1);
  168. }
  169.  
  170. /* Netnews mail find list of all mailboxes
  171.  * Accepts: mail stream
  172.  *        pattern to search
  173.  */
  174.  
  175. void news_find_all (MAILSTREAM *stream,char *pat)
  176. {
  177.   /* Always a no-op */
  178. }
  179.  
  180.  
  181. /* Netnews mail find list of all bboards
  182.  * Accepts: mail stream
  183.  *        pattern to search
  184.  */
  185.  
  186. void news_find_all_bboards (MAILSTREAM *stream,char *pat)
  187. {
  188.   int fd;
  189.   char patx[MAILTMPLEN];
  190.   char *s,*t,*u;
  191.   struct stat sbuf;
  192.   lcase (strcpy (patx,pat));    /* make sure compare with lower case */
  193.   if ((!stat ((char *) mail_parameters (NIL,GET_NEWSSPOOL,NIL),&sbuf)) &&
  194.       ((fd = open ((char *) mail_parameters (NIL,GET_NEWSACTIVE,NIL),O_RDONLY,
  195.            NIL)) >= 0)) {
  196.     fstat (fd,&sbuf);        /* get file size and read data */
  197.     read (fd,s = (char *) fs_get (sbuf.st_size + 1),sbuf.st_size);
  198.     close (fd);            /* close file */
  199.     s[sbuf.st_size] = '\0';    /* tie off string */
  200.     if (t = strtok (s,"\n")) do if (u = strchr (t,' ')) {
  201.       *u = '\0';        /* tie off at end of name */
  202.       if (pmatch (lcase (t),patx)) mm_bboard (t);
  203.     } while (t = strtok (NIL,"\n"));
  204.     fs_give ((void **) &s);
  205.   }
  206. }
  207.  
  208. /* Netnews mail subscribe to mailbox
  209.  * Accepts: mail stream
  210.  *        mailbox to add to subscription list
  211.  * Returns: T on success, NIL on failure
  212.  */
  213.  
  214. long news_subscribe (MAILSTREAM *stream,char *mailbox)
  215. {
  216.   return NIL;            /* never valid for netnews */
  217. }
  218.  
  219.  
  220. /* Netnews mail unsubscribe to mailbox
  221.  * Accepts: mail stream
  222.  *        mailbox to delete from subscription list
  223.  * Returns: T on success, NIL on failure
  224.  */
  225.  
  226. long news_unsubscribe (MAILSTREAM *stream,char *mailbox)
  227. {
  228.   return NIL;            /* never valid for netnews */
  229. }
  230.  
  231.  
  232. /* Netnews mail subscribe to bboard
  233.  * Accepts: mail stream
  234.  *        bboard to add to subscription list
  235.  * Returns: T on success, NIL on failure
  236.  */
  237.  
  238. long news_subscribe_bboard (MAILSTREAM *stream,char *mailbox)
  239. {
  240.   return newsrc_update (mailbox,':');
  241. }
  242.  
  243.  
  244. /* Netnews mail unsubscribe to bboard
  245.  * Accepts: mail stream
  246.  *        bboard to delete from subscription list
  247.  * Returns: T on success, NIL on failure
  248.  */
  249.  
  250. long news_unsubscribe_bboard (MAILSTREAM *stream,char *mailbox)
  251. {
  252.   return newsrc_update (mailbox,'!');
  253. }
  254.  
  255. /* Netnews mail create mailbox
  256.  * Accepts: mail stream
  257.  *        mailbox name to create
  258.  * Returns: T on success, NIL on failure
  259.  */
  260.  
  261. long news_create (MAILSTREAM *stream,char *mailbox)
  262. {
  263.   return NIL;            /* never valid for netnews */
  264. }
  265.  
  266.  
  267. /* Netnews mail delete mailbox
  268.  *        mailbox name to delete
  269.  * Returns: T on success, NIL on failure
  270.  */
  271.  
  272. long news_delete (MAILSTREAM *stream,char *mailbox)
  273. {
  274.   return NIL;            /* never valid for netnews */
  275. }
  276.  
  277.  
  278. /* Netnews mail rename mailbox
  279.  * Accepts: mail stream
  280.  *        old mailbox name
  281.  *        new mailbox name
  282.  * Returns: T on success, NIL on failure
  283.  */
  284.  
  285. long news_rename (MAILSTREAM *stream,char *old,char *new)
  286. {
  287.   return NIL;            /* never valid for netnews */
  288. }
  289.  
  290. /* Netnews mail open
  291.  * Accepts: stream to open
  292.  * Returns: stream on success, NIL on failure
  293.  */
  294.  
  295. MAILSTREAM *news_open (MAILSTREAM *stream)
  296. {
  297.   long i,nmsgs,j,k;
  298.   char c = NIL,*s,*t,tmp[MAILTMPLEN];
  299.   struct direct **names;
  300.                   /* return prototype for OP_PROTOTYPE call */
  301.   if (!stream) return &newsproto;
  302.   if (LOCAL) {            /* close old file if stream being recycled */
  303.     news_close (stream);    /* dump and save the changes */
  304.     stream->dtb = &newsdriver;    /* reattach this driver */
  305.     mail_free_cache (stream);    /* clean up cache */
  306.   }
  307.   lcase (stream->mailbox);    /* build news directory name */
  308.   sprintf (s = tmp,"%s/%s",(char *) mail_parameters (NIL,GET_NEWSSPOOL,NIL),
  309.        stream->mailbox + 1);
  310.   while (s = strchr (s,'.')) *s = '/';
  311.                 /* scan directory */
  312.   if ((nmsgs = scandir (tmp,&names,news_select,news_numsort)) >= 0) {
  313.     stream->local = fs_get (sizeof (NEWSLOCAL));
  314.     LOCAL->dirty = NIL;        /* no update to .newsrc needed yet */
  315.     LOCAL->dir = cpystr (tmp);    /* copy directory name for later */
  316.     LOCAL->name = cpystr (stream->mailbox + 1);
  317.                 /* create cache */
  318.     LOCAL->number = (unsigned long *) fs_get (nmsgs * sizeof (unsigned long));
  319.     LOCAL->header = (char **) fs_get (nmsgs * sizeof (char *));
  320.     LOCAL->body = (char **) fs_get (nmsgs * sizeof (char *));
  321.     for (i = 0; i<nmsgs; ++i) {    /* initialize cache */
  322.       LOCAL->number[i] = atoi (names[i]->d_name);
  323.       fs_give ((void **) &names[i]);
  324.       LOCAL->header[i] = LOCAL->body[i] = NIL;
  325.     }
  326.     fs_give ((void **) &names);    /* free directory */
  327.                 /* make temporary buffer */
  328.     LOCAL->buf = (char *) fs_get ((LOCAL->buflen = MAXMESSAGESIZE) + 1);
  329.     stream->sequence++;        /* bump sequence number */
  330.     stream->rdonly = T;        /* make sure higher level knows readonly */
  331.     mail_exists (stream,nmsgs);    /* notify upper level that messages exist */
  332.                 /* read .newsrc entries */
  333.     mail_recent (stream,newsrc_read (LOCAL->name,stream,LOCAL->number));
  334.                 /* notify if empty bboard */
  335.     if (!(stream->nmsgs || stream->silent)) {
  336.       sprintf (tmp,"Newsgroup %s is empty",LOCAL->name);
  337.       mm_log (tmp,WARN);
  338.     }
  339.   }
  340.   return LOCAL ? stream : NIL;    /* if stream is alive, return to caller */
  341. }
  342.  
  343. /* Netnews file name selection test
  344.  * Accepts: candidate directory entry
  345.  * Returns: T to use file name, NIL to skip it
  346.  */
  347.  
  348. int news_select (struct direct *name)
  349. {
  350.   char c;
  351.   char *s = name->d_name;
  352.   while (c = *s++) if (!isdigit (c)) return NIL;
  353.   return T;
  354. }
  355.  
  356.  
  357. /* Netnews file name comparision
  358.  * Accepts: first candidate directory entry
  359.  *        second candidate directory entry
  360.  * Returns: negative if d1 < d2, 0 if d1 == d2, postive if d1 > d2
  361.  */
  362.  
  363. int news_numsort (struct direct **d1,struct direct **d2)
  364. {
  365.   return (atoi ((*d1)->d_name) - atoi ((*d2)->d_name));
  366. }
  367.  
  368.  
  369. /* Netnews mail close
  370.  * Accepts: MAIL stream
  371.  */
  372.  
  373. void news_close (MAILSTREAM *stream)
  374. {
  375.   if (LOCAL) {            /* only if a file is open */
  376.     news_check (stream);    /* dump final checkpoint */
  377.     if (LOCAL->dir) fs_give ((void **) &LOCAL->dir);
  378.     if (LOCAL->name) fs_give ((void **) &LOCAL->name);
  379.     news_gc (stream,GC_TEXTS);    /* free local cache */
  380.     fs_give ((void **) &LOCAL->number);
  381.     fs_give ((void **) &LOCAL->header);
  382.     fs_give ((void **) &LOCAL->body);
  383.                 /* free local scratch buffer */
  384.     if (LOCAL->buf) fs_give ((void **) &LOCAL->buf);
  385.                 /* nuke the local data */
  386.     fs_give ((void **) &stream->local);
  387.     stream->dtb = NIL;        /* log out the DTB */
  388.   }
  389. }
  390.  
  391. /* Netnews mail fetch fast information
  392.  * Accepts: MAIL stream
  393.  *        sequence
  394.  */
  395.  
  396. void news_fetchfast (MAILSTREAM *stream,char *sequence)
  397. {
  398.   long i;
  399.                 /* ugly and slow */
  400.   if (stream && LOCAL && mail_sequence (stream,sequence))
  401.     for (i = 1; i <= stream->nmsgs; i++)
  402.       if (mail_elt (stream,i)->sequence)
  403.     news_fetchheader (stream,i);
  404. }
  405.  
  406.  
  407. /* Netnews mail fetch flags
  408.  * Accepts: MAIL stream
  409.  *        sequence
  410.  */
  411.  
  412. void news_fetchflags (MAILSTREAM *stream,char *sequence)
  413. {
  414.   return;            /* no-op for local mail */
  415. }
  416.  
  417.  
  418. /* Netnews mail fetch envelope
  419.  * Accepts: MAIL stream
  420.  *        message # to fetch
  421.  *        pointer to return body
  422.  * Returns: envelope of this message, body returned in body value
  423.  *
  424.  * Fetches the "fast" information as well
  425.  */
  426.  
  427. ENVELOPE *news_fetchstructure (MAILSTREAM *stream,long msgno,BODY **body)
  428. {
  429.   char *h,*t;
  430.   LONGCACHE *lelt;
  431.   ENVELOPE **env;
  432.   STRING bs;
  433.   BODY **b;
  434.   if (stream->scache) {        /* short cache */
  435.     if (msgno != stream->msgno){/* flush old poop if a different message */
  436.       mail_free_envelope (&stream->env);
  437.       mail_free_body (&stream->body);
  438.     }
  439.     stream->msgno = msgno;
  440.     env = &stream->env;        /* get pointers to envelope and body */
  441.     b = &stream->body;
  442.   }
  443.   else {            /* long cache */
  444.     lelt = mail_lelt (stream,msgno);
  445.     env = &lelt->env;        /* get pointers to envelope and body */
  446.     b = &lelt->body;
  447.   }
  448.   if ((body && !*b) || !*env) {    /* have the poop we need? */
  449.     mail_free_envelope (env);    /* flush old envelope and body */
  450.     mail_free_body (b);
  451.     h = news_fetchheader (stream,msgno);
  452.                 /* can't use fetchtext since it'll set seen */
  453.     t = LOCAL->body[msgno - 1] ? LOCAL->body[msgno - 1] : "";
  454.     INIT (&bs,mail_string,(void *) t,strlen (t));
  455.                 /* parse envelope and body */
  456.     rfc822_parse_msg (env,body ? b : NIL,h,strlen (h),&bs,BADHOST,LOCAL->buf);
  457.   }
  458.   if (body) *body = *b;        /* return the body */
  459.   return *env;            /* return the envelope */
  460. }
  461.  
  462. /* Netnews mail fetch message header
  463.  * Accepts: MAIL stream
  464.  *        message # to fetch
  465.  * Returns: message header in RFC822 format
  466.  */
  467.  
  468. char *news_fetchheader (MAILSTREAM *stream,long msgno)
  469. {
  470.   unsigned long i,hdrsize;
  471.   int fd;
  472.   char *s,*b;
  473.   long m = msgno - 1;
  474.   struct stat sbuf;
  475.   struct tm *tm;
  476.   MESSAGECACHE *elt = mail_elt (stream,msgno);
  477.                 /* build message file name */
  478.   sprintf (LOCAL->buf,"%s/%lu",LOCAL->dir,LOCAL->number[m]);
  479.   if (!LOCAL->header[m] && ((fd = open (LOCAL->buf,O_RDONLY,NIL)) >= 0)) {
  480.     fstat (fd,&sbuf);        /* get size of message */
  481.                 /* make plausible IMAPish date string */
  482.     tm = gmtime (&sbuf.st_mtime);
  483.     elt->day = tm->tm_mday; elt->month = tm->tm_mon + 1;
  484.     elt->year = tm->tm_year + 1900 - BASEYEAR;
  485.     elt->hours = tm->tm_hour; elt->minutes = tm->tm_min;
  486.     elt->seconds = tm->tm_sec;
  487.     elt->zhours = 0; elt->zminutes = 0;
  488.                 /* slurp message */
  489.     read (fd,s = (char *) fs_get (sbuf.st_size +1),sbuf.st_size);
  490.     s[sbuf.st_size] = '\0';    /* tie off file */
  491.     close (fd);            /* flush message file */
  492.                 /* find end of header */
  493.     for (i = 0,b = s; *b && !(i && (*b == '\n')); i = (*b++ == '\n'));
  494.     hdrsize = (*b ? ++b : b)-s;    /* number of header bytes */
  495.     elt->rfc822_size =        /* size of entire message in CRLF form */
  496.       strcrlfcpy (&LOCAL->header[m],&i,s,hdrsize) +
  497.     strcrlfcpy (&LOCAL->body[m],&i,b,sbuf.st_size - hdrsize);
  498.     fs_give ((void **) &s);    /* flush old data */
  499.   }
  500.   return LOCAL->header[m] ? LOCAL->header[m] : "";
  501. }
  502.  
  503. /* Netnews mail fetch message text (body only)
  504.  * Accepts: MAIL stream
  505.  *        message # to fetch
  506.  * Returns: message text in RFC822 format
  507.  */
  508.  
  509. char *news_fetchtext (MAILSTREAM *stream,long msgno)
  510. {
  511.   long i = msgno - 1;
  512.   MESSAGECACHE *elt = mail_elt (stream,msgno);
  513.                 /* snarf message in case don't have it yet */
  514.   news_fetchheader (stream,msgno);
  515.   elt->seen = T;        /* mark as seen */
  516.   return LOCAL->body[i] ? LOCAL->body[i] : "";
  517. }
  518.  
  519. /* Netnews fetch message body as a structure
  520.  * Accepts: Mail stream
  521.  *        message # to fetch
  522.  *        section specifier
  523.  *        pointer to length
  524.  * Returns: pointer to section of message body
  525.  */
  526.  
  527. char *news_fetchbody (MAILSTREAM *stream,long m,char *s,unsigned long *len)
  528. {
  529.   BODY *b;
  530.   PART *pt;
  531.   unsigned long i;
  532.   char *base;
  533.   unsigned long offset = 0;
  534.   MESSAGECACHE *elt = mail_elt (stream,m);
  535.                 /* make sure have a body */
  536.   if (!(news_fetchstructure (stream,m,&b) && b && s && *s &&
  537.     ((i = strtol (s,&s,10)) > 0) && (base = news_fetchtext (stream,m))))
  538.     return NIL;
  539.   do {                /* until find desired body part */
  540.                 /* multipart content? */
  541.     if (b->type == TYPEMULTIPART) {
  542.       pt = b->contents.part;    /* yes, find desired part */
  543.       while (--i && (pt = pt->next));
  544.       if (!pt) return NIL;    /* bad specifier */
  545.                 /* note new body, check valid nesting */
  546.       if (((b = &pt->body)->type == TYPEMULTIPART) && !*s) return NIL;
  547.       offset = pt->offset;    /* get new offset */
  548.     }
  549.     else if (i != 1) return NIL;/* otherwise must be section 1 */
  550.                 /* need to go down further? */
  551.     if (i = *s) switch (b->type) {
  552.     case TYPEMESSAGE:        /* embedded message, calculate new base */
  553.       offset = b->contents.msg.offset;
  554.       b = b->contents.msg.body;    /* get its body, drop into multipart case */
  555.     case TYPEMULTIPART:        /* multipart, get next section */
  556.       if ((*s++ == '.') && (i = strtol (s,&s,10)) > 0) break;
  557.     default:            /* bogus subpart specification */
  558.       return NIL;
  559.     }
  560.   } while (i);
  561.                 /* lose if body bogus */
  562.   if ((!b) || b->type == TYPEMULTIPART) return NIL;
  563.   elt->seen = T;        /* mark as seen */
  564.   return rfc822_contents (&LOCAL->buf,&LOCAL->buflen,len,base + offset,
  565.               b->size.ibytes,b->encoding);
  566. }
  567.  
  568. /* Netnews mail set flag
  569.  * Accepts: MAIL stream
  570.  *        sequence
  571.  *        flag(s)
  572.  */
  573.  
  574. void news_setflag (MAILSTREAM *stream,char *sequence,char *flag)
  575. {
  576.   MESSAGECACHE *elt;
  577.   long i;
  578.   short f = news_getflags (stream,flag);
  579.   if (!f) return;        /* no-op if no flags to modify */
  580.                 /* get sequence and loop on it */
  581.   if (mail_sequence (stream,sequence)) for (i = 0; i < stream->nmsgs; i++)
  582.     if ((elt = mail_elt (stream,i + 1))->sequence) {
  583.       if (f&fSEEN) elt->seen=T;    /* set all requested flags */
  584.       if (f&fDELETED) {        /* deletion also purges the cache */
  585.     elt->deleted = T;    /* mark deleted */
  586.     LOCAL->dirty = T;    /* mark dirty */
  587.     if (LOCAL->header[i]) fs_give ((void **) &LOCAL->header[i]);
  588.     if (LOCAL->body[i]) fs_give ((void **) &LOCAL->body[i]);
  589.       }
  590.       if (f&fFLAGGED) elt->flagged = T;
  591.       if (f&fANSWERED) elt->answered = T;
  592.     }
  593. }
  594.  
  595.  
  596. /* Netnews mail clear flag
  597.  * Accepts: MAIL stream
  598.  *        sequence
  599.  *        flag(s)
  600.  */
  601.  
  602. void news_clearflag (MAILSTREAM *stream,char *sequence,char *flag)
  603. {
  604.   MESSAGECACHE *elt;
  605.   long i;
  606.   short f = news_getflags (stream,flag);
  607.   if (!f) return;        /* no-op if no flags to modify */
  608.                 /* get sequence and loop on it */
  609.   if (mail_sequence (stream,sequence)) for (i = 0; i < stream->nmsgs; i++)
  610.     if ((elt = mail_elt (stream,i + 1))->sequence) {
  611.                 /* clear all requested flags */
  612.       if (f&fSEEN) elt->seen = NIL;
  613.       if (f&fDELETED) {
  614.     elt->deleted = NIL;    /* undelete */
  615.     LOCAL->dirty = T;    /* mark stream as dirty */
  616.       }
  617.       if (f&fFLAGGED) elt->flagged = NIL;
  618.       if (f&fANSWERED) elt->answered = NIL;
  619.     }
  620. }
  621.  
  622. /* Netnews mail search for messages
  623.  * Accepts: MAIL stream
  624.  *        search criteria
  625.  */
  626.  
  627. void news_search (MAILSTREAM *stream,char *criteria)
  628. {
  629.   long i,n;
  630.   char *d;
  631.   search_t f;
  632.                 /* initially all searched */
  633.   for (i = 1; i <= stream->nmsgs; ++i) mail_elt (stream,i)->searched = T;
  634.                 /* get first criterion */
  635.   if (criteria && (criteria = strtok (criteria," "))) {
  636.                 /* for each criterion */
  637.     for (; criteria; (criteria = strtok (NIL," "))) {
  638.       f = NIL; d = NIL; n = 0;    /* init then scan the criterion */
  639.       switch (*ucase (criteria)) {
  640.       case 'A':            /* possible ALL, ANSWERED */
  641.     if (!strcmp (criteria+1,"LL")) f = news_search_all;
  642.     else if (!strcmp (criteria+1,"NSWERED")) f = news_search_answered;
  643.     break;
  644.       case 'B':            /* possible BCC, BEFORE, BODY */
  645.     if (!strcmp (criteria+1,"CC"))
  646.       f = news_search_string (news_search_bcc,&d,&n);
  647.     else if (!strcmp (criteria+1,"EFORE"))
  648.       f = news_search_date (news_search_before,&n);
  649.     else if (!strcmp (criteria+1,"ODY"))
  650.       f = news_search_string (news_search_body,&d,&n);
  651.     break;
  652.       case 'C':            /* possible CC */
  653.     if (!strcmp (criteria+1,"C")) 
  654.       f = news_search_string (news_search_cc,&d,&n);
  655.     break;
  656.       case 'D':            /* possible DELETED */
  657.     if (!strcmp (criteria+1,"ELETED")) f = news_search_deleted;
  658.     break;
  659.       case 'F':            /* possible FLAGGED, FROM */
  660.     if (!strcmp (criteria+1,"LAGGED")) f = news_search_flagged;
  661.     else if (!strcmp (criteria+1,"ROM"))
  662.       f = news_search_string (news_search_from,&d,&n);
  663.     break;
  664.       case 'K':            /* possible KEYWORD */
  665.     if (!strcmp (criteria+1,"EYWORD"))
  666.       f = news_search_flag (news_search_keyword,&d);
  667.     break;
  668.       case 'N':            /* possible NEW */
  669.     if (!strcmp (criteria+1,"EW")) f = news_search_new;
  670.     break;
  671.  
  672.       case 'O':            /* possible OLD, ON */
  673.     if (!strcmp (criteria+1,"LD")) f = news_search_old;
  674.     else if (!strcmp (criteria+1,"N"))
  675.       f = news_search_date (news_search_on,&n);
  676.     break;
  677.       case 'R':            /* possible RECENT */
  678.     if (!strcmp (criteria+1,"ECENT")) f = news_search_recent;
  679.     break;
  680.       case 'S':            /* possible SEEN, SINCE, SUBJECT */
  681.     if (!strcmp (criteria+1,"EEN")) f = news_search_seen;
  682.     else if (!strcmp (criteria+1,"INCE"))
  683.       f = news_search_date (news_search_since,&n);
  684.     else if (!strcmp (criteria+1,"UBJECT"))
  685.       f = news_search_string (news_search_subject,&d,&n);
  686.     break;
  687.       case 'T':            /* possible TEXT, TO */
  688.     if (!strcmp (criteria+1,"EXT"))
  689.       f = news_search_string (news_search_text,&d,&n);
  690.     else if (!strcmp (criteria+1,"O"))
  691.       f = news_search_string (news_search_to,&d,&n);
  692.     break;
  693.       case 'U':            /* possible UN* */
  694.     if (criteria[1] == 'N') {
  695.       if (!strcmp (criteria+2,"ANSWERED")) f = news_search_unanswered;
  696.       else if (!strcmp (criteria+2,"DELETED")) f = news_search_undeleted;
  697.       else if (!strcmp (criteria+2,"FLAGGED")) f = news_search_unflagged;
  698.       else if (!strcmp (criteria+2,"KEYWORD"))
  699.         f = news_search_flag (news_search_unkeyword,&d);
  700.       else if (!strcmp (criteria+2,"SEEN")) f = news_search_unseen;
  701.     }
  702.     break;
  703.       default:            /* we will barf below */
  704.     break;
  705.       }
  706.       if (!f) {            /* if can't determine any criteria */
  707.     sprintf (LOCAL->buf,"Unknown search criterion: %.80s",criteria);
  708.     mm_log (LOCAL->buf,ERROR);
  709.     return;
  710.       }
  711.                 /* run the search criterion */
  712.       for (i = 1; i <= stream->nmsgs; ++i)
  713.     if (mail_elt (stream,i)->searched && !(*f) (stream,i,d,n))
  714.       mail_elt (stream,i)->searched = NIL;
  715.     }
  716.                 /* report search results to main program */
  717.     for (i = 1; i <= stream->nmsgs; ++i)
  718.       if (mail_elt (stream,i)->searched) mail_searched (stream,i);
  719.   }
  720. }
  721.  
  722. /* Netnews mail ping mailbox
  723.  * Accepts: MAIL stream
  724.  * Returns: T if stream alive, else NIL
  725.  */
  726.  
  727. long news_ping (MAILSTREAM *stream)
  728. {
  729.   return T;            /* always alive */
  730. }
  731.  
  732.  
  733. /* Netnews mail check mailbox
  734.  * Accepts: MAIL stream
  735.  */
  736.  
  737. void news_check (MAILSTREAM *stream)
  738. {
  739.                 /* never do if no updates */
  740.   if (LOCAL->dirty) newsrc_write (LOCAL->name,stream,LOCAL->number);
  741. }
  742.  
  743.  
  744. /* Netnews mail expunge mailbox
  745.  * Accepts: MAIL stream
  746.  */
  747.  
  748. void news_expunge (MAILSTREAM *stream)
  749. {
  750.   if (!stream->silent) mm_log ("Expunge ignored on readonly mailbox",NIL);
  751. }
  752.  
  753. /* Netnews mail copy message(s)
  754.  * Accepts: MAIL stream
  755.  *        sequence
  756.  *        destination mailbox
  757.  * Returns: T if copy successful, else NIL
  758.  */
  759.  
  760. long news_copy (MAILSTREAM *stream,char *sequence,char *mailbox)
  761. {
  762.   mm_log ("Copy not valid for netnews",ERROR);
  763.   return NIL;
  764. }
  765.  
  766.  
  767. /* Netnews mail move message(s)
  768.  * Accepts: MAIL stream
  769.  *        sequence
  770.  *        destination mailbox
  771.  * Returns: T if move successful, else NIL
  772.  */
  773.  
  774. long news_move (MAILSTREAM *stream,char *sequence,char *mailbox)
  775. {
  776.   mm_log ("Move not valid for netnews",ERROR);
  777.   return NIL;
  778. }
  779.  
  780.  
  781. /* Netnews mail append message from stringstruct
  782.  * Accepts: MAIL stream
  783.  *        destination mailbox
  784.  *        stringstruct of messages to append
  785.  * Returns: T if append successful, else NIL
  786.  */
  787.  
  788. long news_append (MAILSTREAM *stream,char *mailbox,char *flags,char *date,
  789.           STRING *message)
  790. {
  791.   mm_log ("Append not valid for netnews",ERROR);
  792.   return NIL;
  793. }
  794.  
  795.  
  796. /* Netnews garbage collect stream
  797.  * Accepts: Mail stream
  798.  *        garbage collection flags
  799.  */
  800.  
  801. void news_gc (MAILSTREAM *stream,long gcflags)
  802. {
  803.   unsigned long i;
  804.   if (gcflags & GC_TEXTS)    /* garbage collect texts? */
  805.                 /* flush texts from cache */
  806.     for (i = 0; i < stream->nmsgs; i++) {
  807.       if (LOCAL->header[i]) fs_give ((void **) &LOCAL->header[i]);
  808.       if (LOCAL->body[i]) fs_give ((void **) &LOCAL->body[i]);
  809.     }
  810. }
  811.  
  812. /* Internal routines */
  813.  
  814.  
  815. /* Parse flag list
  816.  * Accepts: MAIL stream
  817.  *        flag list as a character string
  818.  * Returns: flag command list
  819.  */
  820.  
  821. short news_getflags (MAILSTREAM *stream,char *flag)
  822. {
  823.   char *t,tmp[MAILTMPLEN],err[MAILTMPLEN];
  824.   short f = 0;
  825.   short i,j;
  826.   if (flag && *flag) {        /* no-op if no flag string */
  827.                 /* check if a list and make sure valid */
  828.     if ((i = (*flag == '(')) ^ (flag[strlen (flag)-1] == ')')) {
  829.       mm_log ("Bad flag list",ERROR);
  830.       return NIL;
  831.     }
  832.                 /* copy the flag string w/o list construct */
  833.     strncpy (tmp,flag+i,(j = strlen (flag) - (2*i)));
  834.     tmp[j] = '\0';
  835.     t = ucase (tmp);        /* uppercase only from now on */
  836.  
  837.     while (t && *t) {        /* parse the flags */
  838.       if (*t == '\\') {        /* system flag? */
  839.     switch (*++t) {        /* dispatch based on first character */
  840.     case 'S':        /* possible \Seen flag */
  841.       if (t[1] == 'E' && t[2] == 'E' && t[3] == 'N') i = fSEEN;
  842.       t += 4;        /* skip past flag name */
  843.       break;
  844.     case 'D':        /* possible \Deleted flag */
  845.       if (t[1] == 'E' && t[2] == 'L' && t[3] == 'E' && t[4] == 'T' &&
  846.           t[5] == 'E' && t[6] == 'D') i = fDELETED;
  847.       t += 7;        /* skip past flag name */
  848.       break;
  849.     case 'F':        /* possible \Flagged flag */
  850.       if (t[1] == 'L' && t[2] == 'A' && t[3] == 'G' && t[4] == 'G' &&
  851.           t[5] == 'E' && t[6] == 'D') i = fFLAGGED;
  852.       t += 7;        /* skip past flag name */
  853.       break;
  854.     case 'A':        /* possible \Answered flag */
  855.       if (t[1] == 'N' && t[2] == 'S' && t[3] == 'W' && t[4] == 'E' &&
  856.           t[5] == 'R' && t[6] == 'E' && t[7] == 'D') i = fANSWERED;
  857.       t += 8;        /* skip past flag name */
  858.       break;
  859.     default:        /* unknown */
  860.       i = 0;
  861.       break;
  862.     }
  863.                 /* add flag to flags list */
  864.     if (i && ((*t == '\0') || (*t++ == ' '))) f |= i;
  865.       }
  866.       else {            /* no user flags yet */
  867.     t = strtok (t," ");    /* isolate flag name */
  868.     sprintf (err,"Unknown flag: %.80s",t);
  869.     t = strtok (NIL," ");    /* get next flag */
  870.     mm_log (err,ERROR);
  871.       }
  872.     }
  873.   }
  874.   return f;
  875. }
  876.  
  877. /* Search support routines
  878.  * Accepts: MAIL stream
  879.  *        message number
  880.  *        pointer to additional data
  881.  *        pointer to temporary buffer
  882.  * Returns: T if search matches, else NIL
  883.  */
  884.  
  885. char news_search_all (MAILSTREAM *stream,long msgno,char *d,long n)
  886. {
  887.   return T;            /* ALL always succeeds */
  888. }
  889.  
  890.  
  891. char news_search_answered (MAILSTREAM *stream,long msgno,char *d,long n)
  892. {
  893.   return mail_elt (stream,msgno)->answered ? T : NIL;
  894. }
  895.  
  896.  
  897. char news_search_deleted (MAILSTREAM *stream,long msgno,char *d,long n)
  898. {
  899.   return mail_elt (stream,msgno)->deleted ? T : NIL;
  900. }
  901.  
  902.  
  903. char news_search_flagged (MAILSTREAM *stream,long msgno,char *d,long n)
  904. {
  905.   return mail_elt (stream,msgno)->flagged ? T : NIL;
  906. }
  907.  
  908.  
  909. char news_search_keyword (MAILSTREAM *stream,long msgno,char *d,long n)
  910. {
  911.   return NIL;            /* keywords not supported yet */
  912. }
  913.  
  914.  
  915. char news_search_new (MAILSTREAM *stream,long msgno,char *d,long n)
  916. {
  917.   MESSAGECACHE *elt = mail_elt (stream,msgno);
  918.   return (elt->recent && !elt->seen) ? T : NIL;
  919. }
  920.  
  921. char news_search_old (MAILSTREAM *stream,long msgno,char *d,long n)
  922. {
  923.   return mail_elt (stream,msgno)->recent ? NIL : T;
  924. }
  925.  
  926.  
  927. char news_search_recent (MAILSTREAM *stream,long msgno,char *d,long n)
  928. {
  929.   return mail_elt (stream,msgno)->recent ? T : NIL;
  930. }
  931.  
  932.  
  933. char news_search_seen (MAILSTREAM *stream,long msgno,char *d,long n)
  934. {
  935.   return mail_elt (stream,msgno)->seen ? T : NIL;
  936. }
  937.  
  938.  
  939. char news_search_unanswered (MAILSTREAM *stream,long msgno,char *d,long n)
  940. {
  941.   return mail_elt (stream,msgno)->answered ? NIL : T;
  942. }
  943.  
  944.  
  945. char news_search_undeleted (MAILSTREAM *stream,long msgno,char *d,long n)
  946. {
  947.   return mail_elt (stream,msgno)->deleted ? NIL : T;
  948. }
  949.  
  950.  
  951. char news_search_unflagged (MAILSTREAM *stream,long msgno,char *d,long n)
  952. {
  953.   return mail_elt (stream,msgno)->flagged ? NIL : T;
  954. }
  955.  
  956.  
  957. char news_search_unkeyword (MAILSTREAM *stream,long msgno,char *d,long n)
  958. {
  959.   return T;            /* keywords not supported yet */
  960. }
  961.  
  962.  
  963. char news_search_unseen (MAILSTREAM *stream,long msgno,char *d,long n)
  964. {
  965.   return mail_elt (stream,msgno)->seen ? NIL : T;
  966. }
  967.  
  968. char news_search_before (MAILSTREAM *stream,long msgno,char *d,long n)
  969. {
  970.   return (char) (news_msgdate (stream,msgno) < n);
  971. }
  972.  
  973.  
  974. char news_search_on (MAILSTREAM *stream,long msgno,char *d,long n)
  975. {
  976.   return (char) (news_msgdate (stream,msgno) == n);
  977. }
  978.  
  979.  
  980. char news_search_since (MAILSTREAM *stream,long msgno,char *d,long n)
  981. {
  982.                 /* everybody interprets "since" as .GE. */
  983.   return (char) (news_msgdate (stream,msgno) >= n);
  984. }
  985.  
  986.  
  987. unsigned long news_msgdate (MAILSTREAM *stream,long msgno)
  988. {
  989.   struct stat sbuf;
  990.   struct tm *tm;
  991.   MESSAGECACHE *elt = mail_elt (stream,msgno);
  992.   if (!elt->day) {        /* get date if don't have it yet */
  993.                 /* build message file name */
  994.     sprintf (LOCAL->buf,"%s/%lu",LOCAL->dir,LOCAL->number[msgno - 1]);
  995.     stat (LOCAL->buf,&sbuf);    /* get message date */
  996.     tm = gmtime (&sbuf.st_mtime);
  997.     elt->day = tm->tm_mday; elt->month = tm->tm_mon + 1;
  998.     elt->year = tm->tm_year + 1900 - BASEYEAR;
  999.     elt->hours = tm->tm_hour; elt->minutes = tm->tm_min;
  1000.     elt->seconds = tm->tm_sec;
  1001.     elt->zhours = 0; elt->zminutes = 0;
  1002.   }
  1003.   return (long) (elt->year << 9) + (elt->month << 5) + elt->day;
  1004. }
  1005.  
  1006.  
  1007. char news_search_body (MAILSTREAM *stream,long msgno,char *d,long n)
  1008. {
  1009.   long i = msgno - 1;
  1010.   news_fetchheader (stream,msgno);
  1011.   return LOCAL->body[i] ?
  1012.     search (LOCAL->body[i],strlen (LOCAL->body[i]),d,n) : NIL;
  1013. }
  1014.  
  1015.  
  1016. char news_search_subject (MAILSTREAM *stream,long msgno,char *d,long n)
  1017. {
  1018.   char *t = news_fetchstructure (stream,msgno,NIL)->subject;
  1019.   return t ? search (t,strlen (t),d,n) : NIL;
  1020. }
  1021.  
  1022.  
  1023. char news_search_text (MAILSTREAM *stream,long msgno,char *d,long n)
  1024. {
  1025.   char *t = news_fetchheader (stream,msgno);
  1026.   return (t && search (t,strlen (t),d,n)) ||
  1027.     news_search_body (stream,msgno,d,n);
  1028. }
  1029.  
  1030. char news_search_bcc (MAILSTREAM *stream,long msgno,char *d,long n)
  1031. {
  1032.   ADDRESS *a = news_fetchstructure (stream,msgno,NIL)->bcc;
  1033.   LOCAL->buf[0] = '\0';        /* initially empty string */
  1034.                 /* get text for address */
  1035.   rfc822_write_address (LOCAL->buf,a);
  1036.   return search (LOCAL->buf,(long) strlen (LOCAL->buf),d,n);
  1037. }
  1038.  
  1039.  
  1040. char news_search_cc (MAILSTREAM *stream,long msgno,char *d,long n)
  1041. {
  1042.   ADDRESS *a = news_fetchstructure (stream,msgno,NIL)->cc;
  1043.   LOCAL->buf[0] = '\0';        /* initially empty string */
  1044.                 /* get text for address */
  1045.   rfc822_write_address (LOCAL->buf,a);
  1046.   return search (LOCAL->buf,(long) strlen (LOCAL->buf),d,n);
  1047. }
  1048.  
  1049.  
  1050. char news_search_from (MAILSTREAM *stream,long msgno,char *d,long n)
  1051. {
  1052.   ADDRESS *a = news_fetchstructure (stream,msgno,NIL)->from;
  1053.   LOCAL->buf[0] = '\0';        /* initially empty string */
  1054.                 /* get text for address */
  1055.   rfc822_write_address (LOCAL->buf,a);
  1056.   return search (LOCAL->buf,(long) strlen (LOCAL->buf),d,n);
  1057. }
  1058.  
  1059.  
  1060. char news_search_to (MAILSTREAM *stream,long msgno,char *d,long n)
  1061. {
  1062.   ADDRESS *a = news_fetchstructure (stream,msgno,NIL)->to;
  1063.   LOCAL->buf[0] = '\0';        /* initially empty string */
  1064.                 /* get text for address */
  1065.   rfc822_write_address (LOCAL->buf,a);
  1066.   return search (LOCAL->buf,(long) strlen (LOCAL->buf),d,n);
  1067. }
  1068.  
  1069. /* Search parsers */
  1070.  
  1071.  
  1072. /* Parse a date
  1073.  * Accepts: function to return
  1074.  *        pointer to date integer to return
  1075.  * Returns: function to return
  1076.  */
  1077.  
  1078. search_t news_search_date (search_t f,long *n)
  1079. {
  1080.   long i;
  1081.   char *s;
  1082.   MESSAGECACHE elt;
  1083.                 /* parse the date and return fn if OK */
  1084.   return (news_search_string (f,&s,&i) && mail_parse_date (&elt,s) &&
  1085.       (*n = (elt.year << 9) + (elt.month << 5) + elt.day)) ? f : NIL;
  1086. }
  1087.  
  1088. /* Parse a flag
  1089.  * Accepts: function to return
  1090.  *        pointer to string to return
  1091.  * Returns: function to return
  1092.  */
  1093.  
  1094. search_t news_search_flag (search_t f,char **d)
  1095. {
  1096.                 /* get a keyword, return if OK */
  1097.   return (*d = strtok (NIL," ")) ? f : NIL;
  1098. }
  1099.  
  1100.  
  1101. /* Parse a string
  1102.  * Accepts: function to return
  1103.  *        pointer to string to return
  1104.  *        pointer to string length to return
  1105.  * Returns: function to return
  1106.  */
  1107.  
  1108. search_t news_search_string (search_t f,char **d,long *n)
  1109. {
  1110.   char *end = " ";
  1111.   char *c = strtok (NIL,"");    /* remainder of criteria */
  1112.   if (!c) return NIL;        /* missing argument */
  1113.   switch (*c) {            /* see what the argument is */
  1114.   case '{':            /* literal string */
  1115.     *n = strtol (c+1,d,10);    /* get its length */
  1116.     if ((*(*d)++ == '}') && (*(*d)++ == '\015') && (*(*d)++ == '\012') &&
  1117.     (!(*(c = *d + *n)) || (*c == ' '))) {
  1118.       char e = *--c;
  1119.       *c = DELIM;        /* make sure not a space */
  1120.       strtok (c," ");        /* reset the strtok mechanism */
  1121.       *c = e;            /* put character back */
  1122.       break;
  1123.     }
  1124.   case '\0':            /* catch bogons */
  1125.   case ' ':
  1126.     return NIL;
  1127.   case '"':            /* quoted string */
  1128.     if (strchr (c+1,'"')) end = "\"";
  1129.     else return NIL;
  1130.   default:            /* atomic string */
  1131.     if (*d = strtok (c,end)) *n = strlen (*d);
  1132.     else return NIL;
  1133.     break;
  1134.   }
  1135.   return f;
  1136. }
  1137.